/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.ao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.RoleAO;
import com.lap.scp.admin.bean.TreeDO;
import com.lap.scp.admin.core.ResourcesManager;
import com.lap.scp.admin.core.RoleManager;
import com.lap.scp.admin.domain.ResourcesDO;
import com.lap.scp.admin.domain.RoleDO;
import com.lap.scp.admin.domain.RoleResourcesDO;
import com.lap.scp.admin.query.RoleQueryDO;
import com.lap.scp.admin.support.ResourcesSupport;
import com.lap.scp.admin.utils.ListUtils;

/**
 * <pre>
 * 角色应用对象
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Service("roleAO")
public class DefaultRoleAO extends AbstractAO implements RoleAO {

	@Resource
	private RoleManager roleManager;

	@Resource
	private ResourcesManager resourcesManager;

	@Resource
	private ResourcesSupport resourcesSupport;

	@Override
	public Resp<Integer> deleteById(int roleId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = roleManager.deleteById(roleId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(roleId, e);
		}
		return resp;
	}

	@Override
	public Resp<RoleDO> insertRole(RoleDO roleDO, BindingResult error) {
		Resp<RoleDO> resp = new Resp<>();
		try {
			super.validate(resp, error, roleDO);
			if (resp.isSuccess()) {
				roleManager.insertRole(roleDO);
				resp.setData(roleDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(roleDO, e);
		}
		return resp;
	}

	@Override
	public Resp<RoleDO> queryById(int roleId) {
		Resp<RoleDO> resp = new Resp<>();
		try {
			RoleDO roleDO = roleManager.queryById(roleId);
			resp.setData(roleDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(roleId, e);
		}
		return resp;
	}

	@Override
	public PageResult<RoleDO> queryPage(Page page, RoleQueryDO roleQueryDO) {
		PageResult<RoleDO> pr = new PageResult<RoleDO>();
		try {
			List<RoleDO> list = roleManager.queryPage(page, roleQueryDO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { page, roleQueryDO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateRole(RoleDO roleDO, BindingResult error) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, error, roleDO);
			if (resp.isSuccess()) {
				int flag = roleManager.updateRole(roleDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(roleDO, e);
		}
		return resp;
	}

	@Override
	public Resp<RoleDO> queryForBind(int roleId) {
		Resp<RoleDO> resp = new Resp<>();
		try {
			RoleDO roleDO = roleManager.queryById(roleId);
			if (roleDO != null) {
				List<Integer> resourcesIdList = roleManager.queryResourcesIdById(roleId);
				List<ResourcesDO> queryList = resourcesManager.queryList(null);
				List<TreeDO> treelist = resourcesSupport.getTree(queryList, resourcesIdList);

				roleDO.setTreelist(treelist);
			}
			resp.setData(roleDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(roleId, e);
		}
		return resp;
	}

	@Override
	public Resp<Integer> roleResources(int roleId, String resourcesIds) {
		Resp<Integer> resp = new Resp<>();
		try {
			List<Integer> resourcesIdList = ListUtils.strToIntList(resourcesIds);
			List<RoleResourcesDO> roleResourcesList = new ArrayList<RoleResourcesDO>();
			RoleResourcesDO ur = null;
			for (Integer resourcesId : resourcesIdList) {
				ur = new RoleResourcesDO(roleId, resourcesId);
				roleResourcesList.add(ur);
			}
			Integer flag = roleManager.bindResources(roleId, roleResourcesList);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { roleId, resourcesIds }, e);
		}
		return resp;
	}

}
